package controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import beans.RepositoryBeanRemote;

import dto.RepositoryDTO;

@ManagedBean
@SessionScoped
public class AddUserToRepController {

	@EJB
	RepositoryBeanRemote rb;


	private List<RepositoryDTO> notMemberRepList = new ArrayList<RepositoryDTO>();
	private Integer currentRep;
	private Integer userId;
	private String popUpUser;

	public String getPopUpUser() {
		return popUpUser;
	}

	public void setPopUpUser(String popUpUser) {
		this.popUpUser = popUpUser;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	/**
	 * @return the notMemberRepList
	 */
	public List<RepositoryDTO> getNotMemberRepList() {

		if (userId==null){
			notMemberRepList = rb.getAllRepositories();
		}
		else
		{
			notMemberRepList = rb.getRepositoriesWhereNotMember(userId);
		}
		userId=null;
		return notMemberRepList;
	}

	/**
	 * @param notMemberRepList the notMemberRepList to set
	 */
	public void setNotMemberRepList(List<RepositoryDTO> notMemberRepList) {
		this.notMemberRepList = notMemberRepList;
	}

	/**
	 * @return the currentRep
	 */
	public Integer getCurrentRep() {
		return currentRep;
	}

	/**
	 * @param currentRep the currentRep to set
	 */
	public void setCurrentRep(Integer currentRep) {
		this.currentRep = currentRep;
	}

	/**
	 * @author ieneir
	 * 
	 */

	public String addUserToRep(){
		// /// internationalizare
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);
		LocaleController language = (LocaleController) session
				.getAttribute("language");
		String messageBundleName = "errorMessages.errors";
		if (language.getLanguage() == "ro") {
			messageBundleName += "_ro";
		} else if (language.getLanguage() == "hu") {
			messageBundleName += "_hu";
		} else if (language.getLanguage() == "de") {
			messageBundleName += "_de";
		}
		ResourceBundle messageBundle = ResourceBundle
				.getBundle(messageBundleName);
		// ///		
		if (currentRep!=null){
			if (userId!=null){
				rb.addUserToRepository(userId,currentRep);
				popUpUser = messageBundle.getString("userAS");
				
			}
			else 
				popUpUser = messageBundle.getString("allUsersAI");
		}

		else 
			popUpUser = messageBundle.getString("userIsMember");
		userId=null;
		return "ok";
	}
	public String popUpClose() {
		popUpUser = null;
		return null;
	}
}
